package com.shengde.jinwawaforleader.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hyphenate.chat.EMClient;
import com.shengde.jinwawaforleader.HomeApplication;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.activity.LoginActivity;
import com.shengde.jinwawaforleader.activity.MainActivity;
import com.shengde.jinwawaforleader.base.BaseFragment;
import com.shengde.jinwawaforleader.bean.User;
import com.shengde.jinwawaforleader.model.myJob.instance_message.Constant;
import com.shengde.jinwawaforleader.model.personalCenter.activity.AboutUs;
import com.shengde.jinwawaforleader.model.personalCenter.activity.Help;
import com.shengde.jinwawaforleader.model.personalCenter.activity.KingardenListActivity;
import com.shengde.jinwawaforleader.model.personalCenter.activity.ModifySecret;
import com.shengde.jinwawaforleader.model.personalCenter.activity.PersonalInfoActivity;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProUserInfo;
import com.shengde.jinwawaforleader.views.CircleImageView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 个人中心fragment
 */
public class PersonalCenterFragment extends BaseFragment {
	private RelativeLayout rl_mima;
	private RelativeLayout rl_bangzhu;
	private RelativeLayout rl_women;
	private RelativeLayout rl_gengxin;
	private RelativeLayout rl_tuichu;
	private RelativeLayout rl_change_kg;
	private CircleImageView iv_person;
	String mLogo;
	String mName;
	private TextView tv_name;

	public static int c;
	public static String name2;

	@Override
	public View initView(LayoutInflater inflater) {
		View view = inflater.inflate(R.layout.fragment_personal_center, null);
		return view;
	}

	@Override
	public void initData(Bundle savedInstanceState) {
		rl_mima = (RelativeLayout) view.findViewById(R.id.rl_mima);
		rl_change_kg = (RelativeLayout) view.findViewById(R.id.rl_change_kg);
		rl_bangzhu = (RelativeLayout) view.findViewById(R.id.rl_bangzhu);
		rl_women = (RelativeLayout) view.findViewById(R.id.rl_women);
		rl_gengxin = (RelativeLayout) view.findViewById(R.id.rl_gengxin);
		rl_tuichu = (RelativeLayout) view.findViewById(R.id.rl_tuichu);
		iv_person = (CircleImageView) view.findViewById(R.id.iv_person);
		tv_name = (TextView) view.findViewById(R.id.tv_person_name);
		showUserInfo();
	}

	@Override
	public void listener() {
		//切换幼儿园
		rl_change_kg.setOnClickListener(this);
		//点击修改密码
		rl_mima.setOnClickListener(this);
		//使用帮助
		rl_bangzhu.setOnClickListener(this);
		//关于我们
		rl_women.setOnClickListener(this);
		//版本更新
		rl_gengxin.setOnClickListener(this);
		//退出登陆
		rl_tuichu.setOnClickListener(this);
		//头像
		iv_person.setOnClickListener(this);

	}

	@Override
	public void onWidgetClick(View view) {
		Intent intent = new Intent();
		switch (view.getId()) {
			case R.id.rl_mima:
				intent.setClass(acticity, ModifySecret.class);
				startActivity(intent);
				break;
			case R.id.rl_bangzhu:
				intent.setClass(acticity, Help.class);
				startActivity(intent);
				break;
			case R.id.rl_women:
				intent.setClass(acticity, AboutUs.class);
				startActivity(intent);
				break;
			case R.id.rl_gengxin:
				int a = MainActivity.getAppVersionCode(acticity);
				if (a >= c) {
					HomeApplication.showToastShort("当前版本已是最新版本");
				} else {
					MainActivity.checkUpdate(getActivity());
				}
				break;
			case R.id.rl_tuichu:
				final Dialog dialog = new Dialog(getActivity(), R.style.dialog);
				View contentView = LayoutInflater.from(getActivity()).inflate(
						R.layout.dialog_exit, null);
				dialog.setContentView(contentView);
				dialog.setCanceledOnTouchOutside(true);
				dialog.show();
				TextView ok = (TextView) contentView.findViewById(R.id.button_ok);
				TextView cacel = (TextView) contentView.findViewById(R.id.button_cacel);
				ok.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						User.setIsOnline(acticity, false);
						Intent intent = new Intent();
						intent.setClass(acticity, LoginActivity.class);
						startActivity(intent);
						showOutApp();

						User.logout(acticity);
						EMClient.getInstance().logout(false);
						acticity.finish();
					}


				});
				cacel.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						dialog.dismiss();
					}
				});

				break;
			case R.id.rl_change_kg:
				intent.setClass(acticity, KingardenListActivity.class);
				startActivity(intent);
				break;
			case R.id.iv_person:
				intent.setClass(acticity, PersonalInfoActivity.class);
				startActivityForResult(intent,Constant.REQUEST_CODE);
				break;
		}
	}

	/**
	 * 退出app 接口
	 */
	private void showOutApp() {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/public/cancelToken";
			;
		} else {
			url = BaseProtocol.HOST_BASE + "/public/cancelToken";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("type", "1");
		params.addParameter("id", User.getTeacherId(acticity));
		params.addParameter("token", name2);
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {
			}

			@Override
			public void onCancelled(CancelledException cex) {
			}

			@Override
			public void onFinished() {
			}
		});
	}

	/**
	 * 获取用户头像
	 */
	private void showUserInfo() {
		String url = "";
		if (User.isTest) {
			url = BaseProtocol.HOST_BASE_TEST + "/parent/UserLogo";
		} else {
			url = BaseProtocol.HOST_BASE + "/parent/UserLogo";
		}
		RequestParams params  = BaseProtocol.getRequestParams(url);
		params.addParameter("userid", User.getUserLoginAccount(getActivity()));
		x.http().post(params, new Callback.CommonCallback<String>() {
			@Override
			public void onSuccess(String result) {
				ProUserInfo pro_class_status = new Gson().fromJson(result, ProUserInfo.class);
				if (pro_class_status != null && pro_class_status.getCode() == 0) {
					mLogo = pro_class_status.getLogo();
					mName = pro_class_status.getName();

					Glide.with(getActivity()).load(BaseProtocol.IMG_BASE + mLogo)
							.error(R.drawable.touxiang)
							.into(iv_person);
					tv_name.setText(mName);
				}

			}

			@Override
			public void onError(Throwable ex, boolean isOnCallback) {

			}

			@Override
			public void onCancelled(CancelledException cex) {

			}

			@Override
			public void onFinished() {

			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == Constant.REQUEST_CODE && resultCode == Constant.SETTING_LOGO) {
			showUserInfo();
		}
	}

	public static void cc(int serverVerstion) {
		// TODO Auto-generated method stub
		c = serverVerstion;
	}

	public static void device_token(String device_token) {
		// TODO Auto-generated method stub
		name2 = device_token;
	}
}